#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/9/23 16:04
# @Author  : Crissu
# @Site    : 
# @File    : universal_util.py
# @Software: PyCharm
import os, random

import cv2

'''
    判断路径是否存在，不存在则创建文件夹
'''
def MakeFloder(path):
    if not os.path.exists(path):
        # 如果不存在则创建目录
        # 创建目录操作函数
        os.makedirs(path)
        print(path + '_创建成功')
    else:
        print(path + "_已经存在")

'''
    获取该路径下的所有文件名
    获得具体图片的name
    返回，如：0/tt.jpg
'''
def GetImageSrc(path):       # data/train/0/
    listPath = list()
    for name in os.listdir(path):
        listPath.append(name)
    return listPath    # img.jpg


'''
    获取随机名字
'''
def GetRandomName():
    random1 = random.randint(0, 100000)
    random2 = random.randint(0, 10000)
    random3 = random.randint(0, 1000)
    return str(random1) + "_" + str(random2) + "_" + str(random3)

'''
    从大样本数据中抽一些出来
    fromPath：原路径 例：a/b/c/
    toPath：新路径
    sampleNum：样本数
'''
def selectSomeSample(fromPath, toPath, sampleNum):
    speciesList = os.listdir(fromPath)
    for specie in speciesList:
        picsPath = fromPath + specie + "/"
        toPicsPath = toPath + specie + "/"
        MakeFloder(toPicsPath)  # 创建文件路径
        nameList = os.listdir(picsPath)
        for i in range(sampleNum):
            picPath = picsPath + nameList[i]
            img = cv2.imread(picPath)
            toPicPath = toPicsPath + nameList[i]
            cv2.imwrite(toPicPath, img)
